import { _decorator, CCInteger, Component, Enum,Label, Node, Prefab ,instantiate, input,Input, Vec3} from 'cc';
import { BLOCK_SIZE,playerContraller  } from './playerContraller';
const { ccclass, property } = _decorator;
enum BlockType {
    BT_NONE,
    BT_STONE,
}
enum GameState {
    GS_INIT,
    GS_PLAYING,
    GS_END,
}


@ccclass('gameMananger')

export class gameMananger extends Component {
    @property({type:Prefab})
    public BoxPrefab:Prefab|null = null;

    @property({type:CCInteger})
    public loadLength:number = 50;
    private _load:BlockType[] = [];

    @property({type:Node}) //开始的菜单
    public startMenu:Node|null = null;

    @property({type:playerContraller}) //角色控制器
    public playerCtrl:playerContraller|null = null;

    @property({type:Label})
    public stepLabel:Label|null = null; //步数

    @property({type:CCInteger})
    public moveIndex:number = 0;

    @property({type:Node})
    public StepNode:Node = null;

    start() {
        console.log('gameMananger start',this.startMenu)
        this.setCurState(GameState.GS_INIT);
        this.playerCtrl?.node.on('jumpEnd',this.onPlayerJumpEnd,this)
    }
    private _targetPos:Vec3 = new Vec3();

    update(deltaTime: number) {
        
    }
   
    onMoveUp(event:MouseEvent){
        console.log('onMoveUp')
    }
    generateRoad(){
        this.node.removeAllChildren();
        this._load = [];
        this._load.push(BlockType.BT_STONE);
        for(let i=1;i<this.loadLength;i++){
            if(this._load[i-1] === BlockType.BT_NONE){
                this._load.push(BlockType.BT_STONE);
            }else{
                this._load.push(Math.random()>0.5?1:0);
            }
        }

        for(let j=0;j<this.loadLength;j++){
            let block:Node|null = this.spawnBlockByType(this._load[j]);
            if(block){
                this.node.addChild(block);
                block.setPosition(j*BLOCK_SIZE,230);
            }
        }
    }

    //生成方块放到合适的位置
    spawnBlockByType(type:BlockType):Node|null{
        if(!this.BoxPrefab) return null;
        let block:Node|null = null;
        switch(type){
            case BlockType.BT_STONE:
                block = instantiate(this.BoxPrefab);
                break;
        }
        return block;
    }
    init(){
        if(this.startMenu){
            this.startMenu.active = true;
        }
        this.generateRoad();
        if(this.playerCtrl){
            this.playerCtrl.setInputActive(false);
            this.playerCtrl.node.setPosition(Vec3.ZERO);
            this.playerCtrl.reset();
        }
    }
    onPlayerJumpEnd(moveIndex:number){  
        if(this.stepLabel){
            this.stepLabel.string = moveIndex>= this.loadLength ?'' + this.loadLength : 'Step:' + moveIndex.toString();
        }
        this.moveIndex = moveIndex;
        this.checkResult();
    }

    checkResult(){
        //检查是否成功
        if(this._load[this.moveIndex] === BlockType.BT_NONE ){
            this.setCurState(GameState.GS_END);
            console.error('you lose') 
        } else  {
            console.log('go on');
        }
    }

    //控制游戏状态
    setCurState(state:GameState){
        switch(state){
            case GameState.GS_INIT:
                this.init();
                break;
            case GameState.GS_PLAYING:
                this.playing();
                break;
            case GameState.GS_END:
                this.end();
                break;
        }   
    }
    end(){
        Vec3.add(this._targetPos, Vec3.ZERO, new Vec3(this.moveIndex * BLOCK_SIZE ,0,0) ) //计算出目标位置
        this.startMenu.setPosition(this._targetPos)
        this.StepNode.setPosition(new Vec3(this.moveIndex * BLOCK_SIZE + 100, -80,0))
        this.startMenu.active = true;
    }
    playing(){
        this.startMenu.active = false;
        setTimeout(()=>{
            if(this.playerCtrl){
                this.playerCtrl.setInputActive(true);
                this.playerCtrl.reset();
            }
        },100)
    }
    onStartButtonClick(){
        this.setCurState(GameState.GS_PLAYING);
        console.log("开始游戏");
    }
}


